//
//  MyButton.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/6/13.
//
//

#include "MyButton.h"
#include "MyUtilities.h"

bool MyButton::init(){
    isFocused = false;
    return true;
}

void MyButton::addCallback(cocos2d::CCCallFuncO *mCallback){
    this->callback = mCallback;
    this->callback->retain();
}

CCRect MyButton::getBoundingRect(){
    return CCRectMake(getPositionX(), getPositionY(), buttonSize.width, buttonSize.height);
}

MyButton* MyButton::create(double mX, double mY, double mWidth, double mHeight){
    MyButton* button = MyButton::create();
    button->setPosition(ccp(mX,mY));
    button->buttonSize = CCSizeMake(mWidth, mHeight);
    return button;
}


bool MyButton::isTouchedAt(cocos2d::CCPoint touchPosition){
    return getBoundingRect().containsPoint(touchPosition);
}

void MyButton::updateState(){
    if(isFocused){
        goToHighlightMode();
    }else{
        goToNormalMode();
    }
}
void MyButton::resetState(){
    isFocused = false;
    updateState();
}

void MyButton::goToNormalMode(){
    
    
}
void MyButton::goToHighlightMode(){
    
}

void MyButton::handleTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    CCPoint touchPosition = MyUtilities::getCoCosPointFromTouches(touches);
    if(isTouchedAt(touchPosition)){
        isFocused = true;
    }
    updateState();
}

void MyButton::handleTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    CCPoint touchPosition = MyUtilities::getCoCosPointFromTouches(touches);
    if(isTouchedAt(touchPosition)==false){
        isFocused = false;
    }
    updateState();
}

void MyButton::onTouchButton(){
    
}

CCPoint MyButton::getOriginPoint(){
    return ccp(getPositionX()+buttonSize.width/2,getPositionY()+buttonSize.height/2);
}

void MyButton::handleTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){

    if(isFocused){
        onTouchButton();
        this->runAction(CCSequence::create(callback,NULL));
        isFocused=false;
    }
    updateState();
}

bool MyButton::isTouchedAt(cocos2d::CCSet *touches){
    bool isTouched = false;
    CCSetIterator it = touches->begin();
    for(int i=0;i<touches->count();i++){
        CCTouch* touch = (CCTouch*)(*it);
        CCPoint touchPosition = MyUtilities::getCocosPointFromTouch(touch);
        if(isTouchedAt(touchPosition)){
            isTouched = true;
            break;
        }
        it++;
        
    }
    return isTouched;
}

void MyButton::handleMultiTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if(isTouchedAt(touches)){
        isFocused = true;
    }
    updateState();
}
void MyButton::handleMultiTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
//    CCSetIterator it = touches->begin();
//    bool hasAtLeastOneTouchInside = false;
//    for(int i=0;i<touches->count();i++){
//        CCTouch* touch = (CCTouch*)(*it);
//        CCPoint touchPosition = MyUtilities::getCocosPointFromTouch(touch);
//        if(isTouchedAt(touchPosition)==true){
//            hasAtLeastOneTouchInside=true;
//            break;
//
//        }
//        it++;
//    }
//    if(hasAtLeastOneTouchInside){
//        
//    }else{
//        isFocused=false;
//        CCLOG("lost focused");
//    }
//    updateState();
}
void MyButton::handleMultiTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if(isFocused){
        onTouchButton();
        this->runAction(CCSequence::create(callback,NULL));
        isFocused=false;
    }
    updateState();
}